package com.example.demo2.test.websocket;

import com.example.demo2.test.factory.ConnectionFactory;
import com.example.demo2.test.pong.BinancePong;
import com.example.demo2.test.service.BinanceService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
import okio.ByteString;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 币安websocket连接管理类
 * @author yusong
 * @date
 */
@Data
@Slf4j
public class BinanceWebSocket extends WebSocketListener{

    //	private String uri = "wss://fstream.binance.com/stream";
    //	private String uri = "wss://fstream.yshyqxx.com/stream";

    private BinanceService binanceService;
    //websocket连接
    private WebSocket webSocket;
    //定义一个request信息
    private Request okhttpRequest;
    //定义一个私有的构造函数 防止外部创建
    private BinanceWebSocket() {}
    /**
     * 创建websocket连接
     * @return
     */
    public static BinanceWebSocket createConnectionManage(BinanceService binanceService, String uri) {
        BinanceWebSocket binanceWebSocket = new BinanceWebSocket();
        binanceWebSocket.setBinanceService(binanceService);
        Request request = new Request.Builder().url(uri).build();
        binanceWebSocket.setOkhttpRequest(request);
        binanceWebSocket.connect();
        return binanceWebSocket;
    }

    //开启一个1个线程的连接池 用于检测连接是否正常
    static ExecutorService pool = Executors.newFixedThreadPool(1);
    static BinancePong pong = null;
    /**
     * 提供一个非静态的方法 创建webSocket
     */
    private void connect() {
        webSocket = ConnectionFactory.createWebSocket(okhttpRequest, this);
        pong = new BinancePong(webSocket);
        pool.submit(pong);
    }

    /**
     * 从币安订阅消息
     * @param subscribeStr	订阅的消息
     */
    public void send(String subscribeStr) {
        this.webSocket.send(subscribeStr);
    }



    // 建立socket连接的时候调用
    public void onOpen(WebSocket webSocket, Response response) {
        log.info("websocket链接了");
    }

    // 收到传递String类型参数时调用
    public void onMessage(WebSocket webSocket, String text) {
        binanceService.message(text);
    }

    // 接收ByteString类型数据的时候调用
    public void onMessage(WebSocket webSocket, ByteString bytes) {
    }

    // 连接关闭的时候调用
    public void onClosing(WebSocket webSocket, int code, String reason) {
//        try {
//            Thread.sleep(5000);
//        } catch (InterruptedException e) {
//            log.debug(e.getMessage());
//        }
//        // 每次检测连接不正常之后 5s重连的操作
//        binanceService.binanceMarket();
        log.info("---------------onClosing异常重连");
    }

    // 连接关闭后调用
    public void onClosed(WebSocket webSocket, int code, String reason) {
        log.info("关闭了");
    }

    // 连接失败调用
    public void onFailure(WebSocket webSocket, Throwable t, Response response) {
//        try {
//            Thread.sleep(5000);
//        } catch (InterruptedException e) {
//            log.debug(e.getMessage());
//        }
//        //每次检测连接不正常之后 5s重连的操作
//        binanceService.binanceMarket();
        log.info("---------------onFailure异常重连");
    }

}
